package com.cohesion.bootstrap.listener;

import java.util.ArrayList;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.cohesion.bootstrap.common.MemoryCacheImpl;
import com.forevershine.business.context.ContextBuilder;
import com.forevershine.business.service.impl.ConfigServiceImpl;
import com.forevershine.common.BusinessConstants;
import com.forevershine.common.Config;

public class BootStrapListener implements ServletContextListener{

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO close DB connection pool
		
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		// TODO open DB connection pool
		//initialized frequent used cache;
		initializeMemoryCache();
		
		
	}

	private void initializeMemoryCache() {
		ConfigServiceImpl configSvc = ContextBuilder.getContext().getInstance(
				ConfigServiceImpl.class);
		ArrayList<String> result =new ArrayList<String>();
		Config config =new Config();
		config.setAttribute(BusinessConstants.CATEGORY);
		((ConfigServiceImpl) configSvc).loadConfig(config, (Object)result);
		MemoryCacheImpl.INSTANCE.set(BusinessConstants.CATEGORY, result);
		
		for(String s : result)
		{
			config.setAttribute(s);
			result = new ArrayList<String>();
			((ConfigServiceImpl) configSvc).loadConfig(config, (Object)result);
			MemoryCacheImpl.INSTANCE.set(s, result);
		}
		
	}

}
